package com.xiangmo.travel.Controller;

import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.LikeDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Service.LikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/like")
public class LikeController {
    @Autowired
    private LikeService likeService;

    //点赞评论
    @TokenRequired
    @PostMapping("/add")
    public ResultData<Integer> addLike(@RequestBody LikeDto likeDto, @RequestHeader("Authorization") String token){
        //添加判断，判断用户是否以及点赞过了
        if(likeService.checkCommentIsLike(likeDto) >= 1){
            log.warn("评论：用户已经点过赞却重复请求点赞！");
            return new ResultData<>(CodeAndMsg.LIKE_ERROR.getCode(), CodeAndMsg.LIKE_ERROR.getMessage(),
                    null, token.substring(7));
        }
        int status = likeService.addLike(likeDto);
        if(status >= 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("评论：用户点赞评论失败！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }

    //取消点赞评论
    @TokenRequired
    @DeleteMapping("/cancel")
    public ResultData<Integer> deleteLike(@RequestBody LikeDto likeDto, @RequestHeader("Authorization") String token){
        int status = likeService.deleteLike(likeDto);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("评论：用户取消点赞评论失败！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }
}
